﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using TVT.DataAccess.Repositories;
using System.Transactions;
using TVT.Common.Exceptions;
using TVT.Common.Helpers;

namespace TVT.BizLayer.Entities
{
    [Serializable]
    public class UserRelation : BaseEntity<UserRelation>
    {
        #region attribute

        private static DBRepository repository = DBRepository.GetInstance();

        private int id;
        private Guid userId1;
        private Guid userId2;
        private Enumerators.RelationStatus status;
        private decimal match;

        #endregion

        #region property

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        public Guid UserId1
        {
            get { return userId1; }
            set { userId1 = value; }
        }
        public Guid UserId2
        {
            get { return userId2; }
            set { userId2 = value; }
        }
        public Enumerators.RelationStatus Status
        {
            get { return status; }
            set { status = value; }
        }
        public decimal Match
        {
            get { return match; }
            set { match = value; }
        }

        #endregion

        #region methods

        public override void Insert()
        {
            throw new NotImplementedException();
        }

        public override void Update()
        {
            throw new NotImplementedException();
        }

        public override void Delete()
        {
            repository.DeleteUserRelation(this.id);
        }

        public override void Save()
        {
            if (this.id == 0)
            {
                UserRelation founded = UserRelation.Find(this.userId1, this.userId2);
                if(founded.Id == 0)
                    repository.SaveUserRelation(this.userId1, this.userId2, (int)this.status, this.match);
                else
                {
                    this.id = founded.id;
                    repository.UpdateUserRelation(this.id, this.userId1, this.userId2, (int)this.status, this.match);
                }
            }
            else
            {
                repository.UpdateUserRelation(this.id, this.userId1, this.userId2, (int)this.status, this.match);
            }
        }

        public static UserRelation Find(Guid userId1, Guid userId2)
        {
            return MapToEntity(repository.FindUserRelation(userId1, userId2));
        }

        public static UserRelation Find(int id)
	    {
		    return MapToEntity(repository.FindUserRelation(id));
	    }

        public static List<UserRelation> FindAll(Guid userId, Enumerators.RelationStatus status)
        {
            return MapToEntities(repository.FindAllUserRelations(userId, (int)status));
        }

        #endregion

    }
}